package com.ruoyi.system.service.impl;

import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysClient;
import com.ruoyi.system.domain.SysClientWeightCount;
import com.ruoyi.system.mapper.SysClientMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysClientWeightMapper;
import com.ruoyi.system.domain.SysClientWeight;
import com.ruoyi.system.service.ISysClientWeightService;

/**
 * 智能称重记录Service业务层处理
 * 
 * @author spark
 * @date 2023-08-13
 */
@Service
public class SysClientWeightServiceImpl implements ISysClientWeightService 
{
    @Autowired
    private SysClientWeightMapper sysClientWeightMapper;

    @Autowired
    private SysClientMapper sysClientMapper;

    /**
     * 查询智能称重记录
     * 
     * @param dataId 智能称重记录主键
     * @return 智能称重记录
     */
    @Override
    public SysClientWeight selectSysClientWeightByDataId(Long dataId)
    {
        return sysClientWeightMapper.selectSysClientWeightByDataId(dataId);
    }

    /**
     * 查询智能称重记录列表
     * 
     * @param sysClientWeight 智能称重记录
     * @return 智能称重记录
     */
    @DataScope(deptAlias = "d")
    @Override
    public List<SysClientWeight> selectSysClientWeightList(SysClientWeight sysClientWeight)
    {
        return sysClientWeightMapper.selectSysClientWeightList(sysClientWeight);
    }

    /**
     * 统计智能称重记录列表
     *
     * @param sysClientWeight 智能称重记录
     * @return 统计智能称重
     */
    @DataScope(deptAlias = "d")
    @Override
    public List<SysClientWeightCount> selectSysClientWeightCount(SysClientWeight sysClientWeight)
    {
        List<SysClientWeight> weights = sysClientWeightMapper.selectSysClientWeightList(sysClientWeight);

        Map<Long, DoubleSummaryStatistics> allGoods = weights.stream().collect(Collectors.groupingBy(SysClientWeight::getClientId, Collectors.summarizingDouble(x -> x.getWeight().doubleValue())));
        Map<String, DoubleSummaryStatistics> goods = weights.stream().collect(Collectors.groupingBy(a -> a.getClientId() + "_" + a.getGoodsId(), Collectors.summarizingDouble(x -> x.getWeight().doubleValue())));
        List<SysClientWeightCount> weightCounts = weights.stream().map(a -> {
            SysClientWeightCount wc = new SysClientWeightCount(a.getGoodsId(), a.getGoodsName(), a.getWeight(), a.getWeightUnit(), a.getClientId(),
                    a.getClientName(), a.getLineName(), a.getRoomName(), a.getFactoryName());
            if(allGoods.containsKey(wc.getClientId())) {
                wc.setWeightSum(allGoods.get(wc.getClientId()).getSum());
            }
            String key = wc.getClientId()+"_"+wc.getGoodsId();
            wc.setRowKey(key);
            if(goods.containsKey(key)){
                wc.setWeightCoodsSum(goods.get(key).getSum());
            }
            return wc;
        }).distinct().collect(Collectors.toList());

        //遍历分组后的结果
        return weightCounts;
    }

    /**
     * 新增智能称重记录
     * 
     * @param sysClientWeight 智能称重记录
     * @return 结果
     */
    @Override
    public int inserttWeightReqSeqId(SysClientWeight sysClientWeight)
    {
        sysClientWeight.setCreateTime(DateUtils.getNowDate());
        //序号重复提交
        SysClientWeight sysClientWeight1 = sysClientWeightMapper.selectSysClientWeightByReqSeqId(sysClientWeight.getProp1());
        if(sysClientWeight1!=null){
            return -7;
        }
        SysClient sysClient = sysClientMapper.selectSysClientByClientId(sysClientWeight.getClientId());
        if(sysClient== null){
            //终端ID无效
            return -8;
        }
        sysClientWeight.setDeptId(sysClient.getDeptId());
        return sysClientWeightMapper.insertSysClientWeight(sysClientWeight);
    }

    @Override
    public int insertSysClientWeight(SysClientWeight sysClientWeight)
    {
        sysClientWeight.setCreateTime(DateUtils.getNowDate());
        SysClientWeight sysClientWeight1 = sysClientWeightMapper.selectSysClientWeightByReqSeqId(sysClientWeight.getProp1());
        if(sysClientWeight1!=null){
            return -7;
        }
        return sysClientWeightMapper.insertSysClientWeight(sysClientWeight);
    }

    /**
     * 修改智能称重记录
     * 
     * @param sysClientWeight 智能称重记录
     * @return 结果
     */
    @Override
    public int updateSysClientWeight(SysClientWeight sysClientWeight)
    {
        sysClientWeight.setUpdateTime(DateUtils.getNowDate());
        return sysClientWeightMapper.updateSysClientWeight(sysClientWeight);
    }

    /**
     * 批量删除智能称重记录
     * 
     * @param dataIds 需要删除的智能称重记录主键
     * @return 结果
     */
    @Override
    public int deleteSysClientWeightByDataIds(Long[] dataIds)
    {
        return sysClientWeightMapper.deleteSysClientWeightByDataIds(dataIds);
    }

    /**
     * 删除智能称重记录信息
     * 
     * @param dataId 智能称重记录主键
     * @return 结果
     */
    @Override
    public int deleteSysClientWeightByDataId(Long dataId)
    {
        return sysClientWeightMapper.deleteSysClientWeightByDataId(dataId);
    }
}
